﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Newtonsoft.Json;
using Meeting.Model;
using Components;

namespace Meeting.Web.ashx
{
    public partial class SaveImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string header = "{0}";
            bool jsonP = false;
            try
            {
                //String cb = Request.Params["callback"]; //  
                ////传过来的 callback 参数 假如 Ext.data.JsonP.callback  
                //if (cb != null)
                //{
                //    jsonP = true;
                //    Response.ContentType = "text/javascript";
                //}
                //else
                //{
                //    Response.ContentType = "application/x-json;charset=utf-8";
                //}

                //header = "";
                //if (jsonP)
                //{
                //    header = cb + "({0})";
                //}
                //else
                //{
                //    header = "{0}";
                //}

                var jSetting = new JsonSerializerSettings();
                jSetting.NullValueHandling = NullValueHandling.Ignore;
                Model.MeetingComment c = JsonConvert.DeserializeObject<Model.MeetingComment>(Request["param"], jSetting);
                if (c != null)
                {
                    String savePath = Server.MapPath("~/upload/comment");
                    if (string.IsNullOrEmpty(c.Path))
                    {
                        //按年月生成文件夹
                        c.Path = StrHelp.MakeFolderName();
                    }
                    savePath = savePath + "/" + c.Path;
                    Guid id;
                    if (c.Id != Guid.Empty)
                    {
                        id = c.Id;
                    }
                    else
                    {
                        id = Guid.NewGuid();
                    }

                    string folder = savePath + "/" + c.MeetingId.ToString() + "/" + id.ToString();
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    else
                    {
                        //清空该文件夹内的图片
                        //DirectoryInfo dir = new DirectoryInfo(folder);
                        //dir.Delete(true);

                        //Directory.CreateDirectory(folder);
                    }
                    if (c.ImageList != null)
                    {
                        int j = 0;
                        for (int i = 0; i < c.ImageList.Count; i++)
                        {
                            j = i + 1;
                            FileStream fs = File.Create(folder + "/" + j.ToString() + ".png");
                            fs.Write(c.ImageList[i].baseByte, 0, c.ImageList[i].baseByte.Length);
                            fs.Close();
                        }
                    }

                    // 保存成功之后在数据库中插入一条附注记录
                    BLL.MeetingBLL bll = new BLL.MeetingBLL();
                    if (c.Id != Guid.Empty)
                    {
                        Response.Write(string.Format(header, bll.Update(c)));
                    }
                    else
                    {
                        c.Id = id;
                        Response.Write(string.Format(header, bll.AddComment(c)));
                    }
                }
            }
            catch (Exception ex)
            {
                JsonResponse response = new JsonResponse();
                response.success = false;
                response.Message = ex.Message;
                Response.Write(string.Format(header, JsonConvert.SerializeObject(response)));
            }
        }
    }
}